import {defineStore} from "pinia";
import {ref} from "vue";


export const useShopInfoStore = defineStore('ShopInfo', () => {
    const nearbyList = ref([
        {
            id: 1,
            name: "沃尔码",
            imgUrl: "http://www.dell-lee.com/imgs/vue3/near.png",
            sales: 10000,
            expressLimit: 0,
            expressPrice: 5,
            slogan: "VIP尊享满89元减4元运费券",
            category: [
                {
                    id: 11,
                    name: "新鲜水果",
                    list: [
                        {
                            id: 111,
                            title: "番茄 250g / 份",
                            imgName: "tomato",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 3.6,
                            oldPrice: 5.6
                        },
                        {
                            id: 112,
                            title: "车厘子 500g / 份",
                            imgName: "cherry",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 33.9,
                            oldPrice: 39.9
                        },
                        {
                            id: 113,
                            title: "橙子 500g / 份",
                            imgName: "orange",
                            sales: 110,
                            number: 0,
                            checked: false,
                            price: 12.9,
                            oldPrice: 22.9
                        }
                    ]
                },
                {
                    id: 12,
                    name: "海鲜水产",
                    list: [
                        {
                            id: 121,
                            title: "帝王蟹 250g / 份",
                            imgName: "crab",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 99.9,
                            oldPrice: 199.9
                        }
                    ]
                }
            ],
            cartList: []
        },
        {
            id: 2,
            name: "山姆会员商店",
            imgUrl: "http://www.dell-lee.com/imgs/vue3/near.png",
            sales: 2000,
            expressLimit: 0,
            expressPrice: 5,
            slogan: "联合利华洗护满10减5",
            category: [
                {
                    id: 21,
                    name: "新鲜水果",
                    list: [
                        {
                            id: 211,
                            title: "番茄 250g / 份",
                            imgName: "tomato",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 3.6,
                            oldPrice: 5.6
                        },
                        {
                            id: 212,
                            title: "桃子 500g / 份",
                            imgName: "tomato",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 33.9,
                            oldPrice: 39.9
                        },
                        {
                            id: 213,
                            title: "橙子 500g / 份",
                            imgName: "orange",
                            sales: 110,
                            number: 0,
                            checked: false,
                            price: 12.9,
                            oldPrice: 22.9
                        }
                    ]
                },
                {
                    id: 22,
                    name: "海鲜水产",
                    list: [
                        {
                            id: 221,
                            title: "帝王蟹 250g / 份",
                            imgName: "crab",
                            sales: 9,
                            number: 0,
                            checked: false,
                            price: 99.9,
                            oldPrice: 199.9
                        },
                        {
                            id: 222,
                            title: "虾 250g / 份",
                            imgName: "tomato",
                            sales: 19,
                            number: 0,
                            checked: false,
                            price: 39.9,
                            oldPrice: 59.9
                        }
                    ]
                },
                {
                    id: 23,
                    name: "零食小吃",
                    list: [
                        {
                            id: 231,
                            title: "薯片 250g / 份",
                            imgName: "tomato",
                            sales: 20,
                            number: 0,
                            checked: false,
                            price: 9.9,
                            oldPrice: 19.9
                        },
                        {
                            id: 232,
                            title: "饼干 250g / 份",
                            imgName: "tomato",
                            sales: 10,
                            number: 0,
                            checked: false,
                            price: 3.9,
                            oldPrice: 5.9
                        }
                    ]
                }
            ],
            cartList: []
        }
    ]);

    const handleClick = () => {
        console.log('click');
    };
    const plusNum = (goodId, shopInfo) => {
        shopInfo.value.category.forEach((item, index) => {
            if (item.list.find(item => item.id === goodId)) {
                shopInfo.value.category[index].list.forEach((item) => {
                    if (item.id === goodId) {
                        item.number++;
                        // console.log(shopInfo.value.category[index].list)
                        // shopInfo.value.category[index].list[0].checked = item.number!==0
                        item.checked = item.number !== 0
                    }
                })
            }
            addCart(shopInfo, item)
        })
    };

    const minusNum = (goodId, shopInfo) => {
        shopInfo.value.category.forEach((item, index) => {
            if (item.list.find(item => item.id === goodId)) {
                shopInfo.value.category[index].list.forEach((item) => {
                    if (item.id === goodId) {
                        if (item.number === 0) return alert("商品数量不能为负数");
                        item.number--;
                        // shopInfo.value.category[index].list[0].checked = item.number !== 0
                        item.checked = item.number !== 0
                    }
                });
            }
            addCart(shopInfo, item)
        });
    };

    function addCart(shopInfo, goodInfo) {

        // console.log(goodInfo.list)
        goodInfo.list.forEach((item) => {
            // console.log(item)
            if (item.checked) {
                // shopInfo.value.cartList.push(item)
                // console.log(item.id);
                const goodID = item.id;
                shopInfo.value.cartList = shopInfo.value.cartList.filter(item => item.id !== goodID)
                // if (shopInfo.value.cartList.find(item => item.id === goodID)) {
                //     return;
                // }
                shopInfo.value.cartList.push(item);
                // item.checked=false

            } else if (!item.checked) {
                const goodID = item.id;
                shopInfo.value.cartList = shopInfo.value.cartList.filter(item => item.id !== goodID)
            }
        });
        shopInfo.value.cartList.sum = 0;
        shopInfo.value.cartList.forEach((item) => {
            //保留小数点后两位
            shopInfo.value.cartList.sum += Number((item.number * item.price).toFixed(2));
        });
    }

    function resetCart(shopInfo) {
        shopInfo.value.cartList = [] // 购物车清空
        shopInfo.value.category.forEach(item => {
                item.list.forEach(item => {
                    item.number = 0; // 商品数量清空
                    item.checked = false; // 商品选中状态清空
            })
        })
    }

// watch([nearbyList.value], (newVal, oldVal) => {
//     nearbyList.value.forEach(item => {
//             let cartList;
//         item.category.forEach(item => {
//             // console.log(item.list)
//             cartList = item.list.filter(item => item.checked);
//         })
//         // console.log(cartList)
//         // console.log(item.cartList=cartList)
//     })
// })
const orderList = ref([]);
    return {
        orderList,
        nearbyList,
        plusNum,
        minusNum,
        handleClick,
        resetCart
    }
});